<template>
  <div>
    <!-- 在table中对form进行编辑   -->
    <el-form :model="modelData">
      <el-table :data="modelData.tableData" style="width: 100%">
        <el-table-column prop="username" label="用户" width="">
          <template slot-scope="scope">
            <!-- required必录 -->
            <el-form-item label="用户名" :prop="'tableData.'+scope.$index +'.username'" :rules="rules.username">
              <el-input v-model="scope.row.username"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="password" label="密码" width="">
          <template slot-scope="scope">
            <el-form-item label="密码" :prop="'tableData.'+scope.$index +'.password'"  :rules="rules.password">
              <el-input v-model="scope.row.password"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="">
          <template slot-scope="scope">
            <el-form-item label="年龄" :prop="'tableData.'+scope.$index +'.age'" :rules="rules.age">
              <el-input v-model="scope.row.age"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="">
          <template slot-scope="scope">
            <!-- 能够实现table中表单校验的方式，rules必录 -->
            <el-form-item label="性别" :prop="'tableData.'+scope.$index +'.sex'" :rules="rules.sex">
              <el-input v-model="modelData.tableData[scope.$index].sex"></el-input>
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column label="Operations">
          <template slot-scope="scope">
            <el-button size="" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <el-button @click="addRow">+ 新增</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      modelData: {
        tableData: [
          {
            username: 'user1',
            password: '123',
            age: '12',
            sex: '1'
          },
          {
            username: 'user2',
            password: '123',
            age: '3',
            sex: '2'
          },
          // 可以添加更多数据
        ]
      },
      rules: {
        username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
        password: [{required: true, message: '密码不能为空', trigger: 'blur'}],
        age: [{required: true, message: '年龄不能为空', trigger: 'blur'}],
        sex: [{required: true, message: '性别不能为空', trigger: 'blur'}],
      },

    };
  },
  methods: {
    addRow() {
      this.modelData.tableData.push({
        username: '',
        password: '',
        age: '',
        sex: ''
      })
    },
    handleEdit(index, row) {
      console.log(index, row, this.modelData.tableData);
      // 在这里实现编辑逻辑，比如打开模态框进行编辑
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.modelData.tableData.splice(index, 1);
    }
  }
};
</script>

<style scoped>
/* 可选的样式调整 */
</style>
